//
//  Team9TopViewController.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/12.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9TopViewController.h"

#import <MBProgressHUD.h>
#import "JQAFNetworkTool.h"
#import "UIImageView+WebCache.h"

#import "Team9TopModel.h"
#import "Team9ItemsModel.h"
#import "Team9BrowseTableViewCell.h"
#import "Team9SubjectTableViewCell.h"
#import "Team9BrowseScrollViewModel.h"
#import "Team9SubjectViewController.h"
#import "Team9SubjectOtherViewController.h"
#import "Team9workVc.h"
#import "Team9DesignerVc.h"
#import "Team9DesignVC.h"
#import <MJRefresh.h>
#import "Team9InspirationController.h"
#import "JQScorllView.h"

#define WID 375.0

@interface Team9TopViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isupLoding;
@property (nonatomic, strong) NSMutableArray *picArr;
@property (nonatomic, strong) NSMutableArray *itemArr;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) JQScorllView *scrollView;
@property (nonatomic, assign) NSInteger count;


@end

@implementation Team9TopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"美丽家";
    //创建tableView加头视图
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEGHT - 49) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    [self.tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:self.tableview];
    self.page = 2;
    self.isupLoding = NO;
    
    self.empty = [Team9EmptyTool toolWithScrollView:self.tableview];
    [self createData];
    self.count = 20;
    
}

#pragma mark 解析首页轮播图数据
-(void)createData{
    [Team9RefreshTool getUrl:@"http://api.meilijia.com/app/featured_page3?page=1" success:^(id result) {
        NSDictionary *dicData = result[@"data"];
        NSDictionary *dicfeature = dicData[@"featured_banners2"];
        self.picArr  = [Team9BrowseScrollViewModel baseModelByArr:dicfeature[@"banners"]].mutableCopy;
        [self scroll];
        [self.tableview.header endRefreshing];
        [self createData1];
    }scrollView:self.tableview empty:self.empty];
}

#pragma mark 解析首页其他数据
- (void)createData1{
    self.tool = [Team9RefreshTool toolWithScrollView:self.tableview url:@"http://api.meilijia.com/app/featured_page3?page=1" style:^NSArray *(id result) {
         NSDictionary *dicData = result[@"data"];
        NSDictionary *dicitem = dicData[@"featured_items"];
       
        return [Team9ItemsModel baseModelByArr:dicitem[@"items"]];

    } refresh:^(NSArray *array) {
        self.itemArr = array.mutableCopy;
        [self.tableview reloadData];
    } empty:self.empty];
}


#pragma mark 轮播图创建
-(void)scroll{
     CGRect frame = CGRectMake(0, 0, WIDTH, 200 / WID * WIDTH);
   self.scrollView = [JQScorllView jq_scrollViewWithFram:frame OnSubview:self.tableview];
    NSMutableArray *array = @[].mutableCopy;
    for (Team9BrowseScrollViewModel *model in self.picArr) {
        [array addObject:model.pic];
    }
    [self.scrollView jq_reloadByArray:array];
    [self.scrollView addTimer];
}

#pragma mark 视图出现时轮播图添加计时器
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.scrollView addTimer];
}

#pragma mark 视图消失时轮播图移除计时器
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.scrollView removeTimer];
}

#pragma mark  TableView返回数组个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemArr.count;
}

#pragma mark TableViewCell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Team9ItemsModel *items = self.itemArr[indexPath.row];
    if ([items.item_type isEqualToString:@"collection" ]) {
        static NSString *collectionReuse = @"collectionReuse";
        Team9BrowseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:collectionReuse];
        if (!cell) {
            cell = [[Team9BrowseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:collectionReuse];
        }
        //判断row,区分自定义cell和tableView的高度
//        [cell.imageviewBig sd_setImageWithURL:[NSURL URLWithString:items.cover_pic] placeholderImage:[UIImage imageNamed:@"111"]];
//        [cell.imageview sd_setImageWithURL:[NSURL URLWithString:items.author_face] placeholderImage:[UIImage imageNamed:@"111"]];
        [Team9Tool sd_progress_imageView:cell.imageviewBig image:items.cover_pic placeholderImage:@"111"];
        [Team9Tool sd_imageView:cell.imageview image:items.author_face placeholderImage:@"333"];
        cell.labelname.text = items.title;
        cell.labeldetail.text = items.title_sub;
        NSString *str = [NSString stringWithFormat:@"%@元/平米",items.work_design_price];
        cell.labelPrice.text=str;
        cell.imageviewSmall.image = [UIImage imageNamed:@"喜欢"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if ([items.item_type isEqualToString:@"subject"]){
        static NSString *re = @"re";
        Team9SubjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:re];
        if (!cell) {
            cell = [[Team9SubjectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:re];
        }
//        [cell.imageview sd_setImageWithURL:[NSURL URLWithString:items.pic] placeholderImage:[UIImage imageNamed:@"111"]];
        [Team9Tool sd_progress_imageView:cell.imageview image:items.pic placeholderImage:@"111"];
        cell.labelTitle.text = items.title;
        NSString *str = [NSString stringWithFormat:@"%@%@",items.col_count,items.col_count_text];
        cell.labelsubTitle.text = str;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
         if ([items.type isEqualToString:@"2"]) {
            cell.labelsubTitle.backgroundColor = [UIColor purpleColor];
        }else if ([items.type isEqualToString:@"1"]){
            cell.labelsubTitle.backgroundColor = [UIColor redColor];
        }
        
        return cell;
    }else if ([items.item_type isEqualToString:@"page"]){
        static NSString *reuse = @"reuse";
        Team9SubjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9SubjectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
//        [cell.imageview sd_setImageWithURL:[NSURL URLWithString:items.pic] placeholderImage:[UIImage imageNamed:@"111"]];
        [Team9Tool sd_progress_imageView:cell.imageview image:items.pic placeholderImage:@"111"];
        cell.labelTitle.text = items.title;
        cell.labelsubTitle.text = @"京沪小资最爱";
        cell.labelsubTitle.textColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
        return nil;
}

#pragma mark 返回cell的自适应高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Team9ItemsModel *items = self.itemArr[indexPath.row];
    if ([items.item_type isEqualToString:@"collection"]) {
        return 270 / WID * WIDTH;
    }else{
        return 200 / WID * WIDTH;
    }
}

#pragma mark tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Team9ItemsModel *items = self.itemArr[indexPath.row];
        if ([items.item_type isEqualToString:@"subject"] && [items.type isEqualToString:@"2"]) {
            //灵感图片
            Team9SubjectViewController *subVC = [[Team9SubjectViewController alloc] init];
            [self.navigationController pushViewController:subVC animated:YES];
            subVC.ID = items.id;
            subVC.str = items.title;
        }else if ([items.item_type isEqualToString:@"subject"] && [items.type isEqualToString:@"1"]){
            //设计师作品
            Team9SubjectOtherViewController *otherVC = [[Team9SubjectOtherViewController alloc] init];
            [self.navigationController pushViewController:otherVC animated:YES];
            otherVC.ID = items.id;
            otherVC.str = items.title;
        }else if ([items.item_type isEqualToString:@"collection"] && [items.type isEqualToString:@"work"]){
            Team9DesignVC *designVC = [[Team9DesignVC alloc] init];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:designVC animated:YES];
            self.hidesBottomBarWhenPushed = NO;
            Team9ItemsModel *items = self.itemArr[indexPath.row];
            NSString *str = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page2?col_id=%ld&tab=photo", items.id];
            designVC.url = str;
        }else if ([items.item_type isEqualToString:@"page"]){
            NSString *str = @"%E5%AE%A2%E5%8E%85-%E7%BE%8E%E5%BC%8F";
            [self.tabBarController setSelectedIndex:1];
            NSDictionary *dic = @{@"url":str,@"type": @"美式-客厅"};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"changePage" object:nil userInfo:dic];
        }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
